/**
 * @filename:CustomerController 2021-11-09 01:40:36
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.Customer;
import com.lwl.blue.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 客人API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
// @Api(tags = "客人" )
@RestController
@RequestMapping("/customer")
@Slf4j
public class CustomerController extends BaseController {
    @Autowired
    CustomerService customerService;

    @PostMapping("/pages")
    public PageResult<Customer> getPages(@RequestBody PageParam<Customer> pageParam) {
        JsonResult<Page<Customer>> pages = customerService.pages(pageParam);
        PageResult<Customer> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("/select")
    public JsonResult select() {
        List<Customer> reList = customerService.list(new QueryWrapper<Customer>()
                .lambda().eq(Customer::getIsDelete, 0));
        return new JsonResult().success(reList);
    }


    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Customer customer) {
        setInsetOrUpdateUser(customer);
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Customer::getCustomerNo, customer.getCustomerNo())
                .eq(Customer::getIsDelete, 0);
        if (null != customer.getId()) {
            wrapper.lambda().notIn(Customer::getId, customer.getId());
        }
        int count = customerService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.DATA_EXISTS_ERROR.getCode(), ErrorCode.DATA_EXISTS_ERROR.getMsg() + customer.getCustomerNo());
        }
        customerService.saveOrUpdate(customer);
        return new JsonResult().success("请求成功");

    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        customerService.update(new UpdateWrapper<Customer>().lambda()
                .set(Customer::getIsDelete, Const.IS_DELETE_YES)
                .set(Customer::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(Customer::getId, id));
        return new JsonResult().success("请求成功");
    }
}
